// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Linq.Expressions;

namespace SnapObjects.Expressions
{
    internal class CompileNestParseProvider : INestParseProvider<Expression>
    {
        public virtual string[] SupportKinds => new[] { SourceKind.Default };

        public virtual ParserOptions<Expression> CreateOptions(ParserOptions<Expression> options)
        {
            return new CompileParserOptions(
                null,
                SourceKind.Default,
                ((CompileParserOptions)options).DeclaredType,
                options.DataSource,
                options.CustomData);
        }

        public virtual Expression Invoke(
            ParserOptions<Expression> options,
            Expression nestExpression,
            ParserOptions<Expression> nestOptions)
        {
            var exprOptions = (CompileParserOptions)options;
            var exprNestOptions = (CompileParserOptions)nestOptions;

            foreach (var used in exprNestOptions.UsedParameters)
            {
                exprOptions.AddUsedParameter(
                    used, used == exprNestOptions.SubModelParameter);
            }

            return nestExpression;
        }
    }
}
